package edu.ustb.aur.rightpackage;

import edu.ustb.aur.myenum.RightEnum;

import java.util.HashMap;
/**
 * @author
 * 用户所有的权利聚合
 */
public class OwnerShip implements IShip {

    /**
     * 使用权
     */
    private UseRight useRight;
    /**
     * 占有权
     */
    private PossessRight possessRight;
    /**
     * 收益权
     */
    private UsufructRight usufructRight;
    /**
     * 处分权
     */
    private DisposeRight disposeRight;

    /**
     * 用户自定义权聚合
     */
    private OtherShip otherShip;

    /**
     * 构造方法
     */
    public OwnerShip() {
        this.useRight = new UseRight();
        this.possessRight = new PossessRight();
        this.usufructRight = new UsufructRight();
        this.disposeRight = new DisposeRight();
        this.otherShip = new OtherShip();
    }

    /**
     * 带参构造函数
     */
    public OwnerShip(UseRight useRight, PossessRight possessRight, UsufructRight usufructRight, DisposeRight disposeRight, OtherShip otherShip) {
        this.useRight = useRight;
        this.possessRight = possessRight;
        this.usufructRight = usufructRight;
        this.disposeRight = disposeRight;
        this.otherShip = otherShip;
    }

    /**
     * 使用isSplitMap为每一个基本权利设置可分属性
     *
     * @param isSplitMap 权利枚举与是否可分的属性对应
     */
    @Override
    public void setEveyRightIsSplit(HashMap<RightEnum, Boolean> isSplitMap) {
        for (RightEnum rightEnum : isSplitMap.keySet()) {
            switch (rightEnum) {
                case USE_RIGHT:
                    useRight.setIsSpilt(isSplitMap.get(rightEnum));
                    break;
                case DISPOSE_RIGHT:
                    disposeRight.setIsSpilt(isSplitMap.get(rightEnum));
                    break;
                case POSSESS_RIGHT:
                    possessRight.setIsSpilt(isSplitMap.get(rightEnum));
                    break;
                case USUFRUCT_RIGHT:
                    usufructRight.setIsSpilt(isSplitMap.get(rightEnum));
                    break;
                case OTHERSHIPE_RIGHT:
                    // TODO : 考虑用户传入自定义自定义的情况
                    break;
                default:
                    break;
            }
        }
    }


    /** bzs
     *  做初始化，初始化可分属性，初始化权利列表
     */
    @Override
    public void init(HashMap<RightEnum, Boolean> isSplitMap, HashMap<RightEnum, AbstractRight> rightUserMap) {
        setEveyRightIsSplit(isSplitMap);
        initAllRightAndUser(rightUserMap);
    }

    /**
     * 初始化所有基本权利
     *
     * @param rightUserMap  一个权利对应多个用户占比 可能有多个用户  <right, <user, percent> >
     */
    public void initAllRightAndUser(HashMap<RightEnum, AbstractRight> rightUserMap) {
        for (RightEnum rightEnum : rightUserMap.keySet()) {
            for (String userAddress : rightUserMap.get(rightEnum).rightMap.keySet()) {
                rightUserMap.get(rightEnum).initRight(userAddress,rightUserMap.get(rightEnum).rightMap.get(userAddress));
            }
        }
    }

    public UseRight getUseRight() {
        return useRight;
    }

    public PossessRight getPossessRight() {
        return possessRight;
    }

    public UsufructRight getUsufructRight() {
        return usufructRight;
    }

    public DisposeRight getDisposeRight() {
        return disposeRight;
    }
}
